<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Operation\Repository;

use Doctrine\ORM\EntityRepository;
use Operation\Entity\CouponCode;
use Operation\Entity\CouponCodeRule;

class CouponCodeRuleRepository extends EntityRepository
{
    /**
     * 优惠码规则列表
     * @return int|mixed|string
     */
    public function findCouponCodeRuleList()
    {
        $query = $this->getEntityManager()->createQueryBuilder();
        $query->select('r')->addSelect('(SELECT COUNT(c.couponCodeId) FROM '.CouponCode::class.' AS c WHERE c.couponCodeRuleId=r.couponCodeRuleId AND c.userId > 0) AS useCodeNum')
            ->from(CouponCodeRule::class, 'r')
            ->orderBy('r.couponCodeRuleId', 'DESC');

        return $query->getQuery()->getResult();
    }
}